//
//  DDInvoiceHistoryDetailVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 2016/10/19.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class DDInvoiceHistoryDetailVC: LDBaseCustomizeVC {

    
    var codeStr = ["bdafdfds"]
    
    var model:DDInvoiceHistoryListModel? {
        
        didSet{
            
            
        }
    }
    
    var infoModel:DDInvoiceInfoModelList?
    
    var contentTitleStr = ["开票抬头","联  系  人","联系方式","所在地区","详细地址"]
    var contentDescStr = ["dd开d票抬头","dd联系人","ff联系方式","所在地区","ff详细地址"]
    
    var dataTitleStr = ["申请时间","开票时间","开票金额","开票张数"]
    var dataDescStr = ["申请时间","开票时间","开票金额","开票张数"]
    
    private var topBar:UILabel?
    private var mianDetailView:LDBaseListTextField?
    private var dateView:LDBaseListTextField?
    private var submitBtn:UIButton?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        requestNet()
        initUI()
        
    }
    
    //请求抬头信息等数据
    func requestNet() {
        
        
        LDUserInfoNet.sharedInstance.getInvoiceBillHistoryListDetail(model?.id, succ: { [weak self] (dictArr) in
            
            let infomodel = DDInvoiceInfoModel.yy_modelWithDictionary(dictArr) ?? DDInvoiceInfoModel()
            self?.infoModel = infomodel.list.first
            self?.updateUI()
            
        }) { (err) in
            
            LDShowHUB.shareInstance.showErr(err as? String ?? "请求失败")
        }
    }
    
    func updateUI() {
        
        topBar?.text = "    开票信息编号: " + (model?.no ?? "")
        
        let mianDescArr = [infoModel?.name ?? "", infoModel?.link_name ?? "",infoModel?.link_mobile ?? "",infoModel?.province ?? "", infoModel?.dist ?? ""]
        mianDetailView?.descTitleArr = mianDescArr
        
        
        let number = infoModel?.number ?? "0"
        let dateDescArr = [infoModel?.dt ?? "",infoModel?.dealdt ?? "",infoModel?.money ?? "",number]
        dateView!.descTitleArr = dateDescArr
        
    }
    
    //删除操作
    func deleteInvoiceHistory() {
        
        LDUserInfoNet.sharedInstance.deleteInvoiceBillHistory(model?.id ?? "", succ: { [weak self] (msg) in
            
            LDShowHUB.shareInstance.showSucc(msg ?? "删除成功")
            self?.navigationController?.popViewControllerAnimated(true)
            
        }) { (err) in
            
            LDShowHUB.shareInstance.showErr(err as?String ?? "删除失败")
        }
    }
}


extension DDInvoiceHistoryDetailVC {
    func initUI() {
        
        view.backgroundColor = LDGolbalColor.BgGoundGray
        
        topBar = UILabel.setBaseLab("    开票多发点啥法", fontSize: 15, titleColor: LDFontColor.Black, Alignment: .Left)
        topBar?.backgroundColor = UIColor.whiteColor()
        
        
        mianDetailView = LDBaseListTextField.init(listTitleArr: contentTitleStr,type:.Show)
        mianDetailView!.descTitleArr = contentDescStr
        mianDetailView?.isCanEdit = false
        mianDetailView?.backgroundColor = UIColor.whiteColor()
        
        
        dateView = LDBaseListTextField.init(listTitleArr: dataTitleStr,type:.Show)
        dateView?.isCanEdit = false
        dateView?.backgroundColor = UIColor.whiteColor()
        submitBtn = UIButton.setDetailsBtn("删除当前记录", picName: nil, btnBackColor: LDGolbalColor.Green, titleColor: LDGolbalColor.White)
        
        view.addSubview(topBar!)
        view.addSubview(mianDetailView!)
        view.addSubview(dateView!)
        view.addSubview(submitBtn!)
        
        submitBtn?.rac_signalForControlEvents(UIControlEvents.TouchUpInside).subscribeNext({[weak self]  (obj) in
            
            self?.deleteInvoiceHistory()
            
        })
        
        
        topBar?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(LDMargin_V*2)
            make.left.right.equalTo(0)
            make.height.equalTo(45)
        })
        
        mianDetailView?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(topBar!.snp_bottom).offset(LDMargin_V*2)
            make.left.right.equalTo(0)
            make.height.equalTo(mianDetailView!.getH())
        })
        
        dateView?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(mianDetailView!.snp_bottom).offset(LDMargin_V*2)
            make.left.right.equalTo(0)
            make.height.equalTo(dateView!.getH())
        })
        
        submitBtn!.snp_makeConstraints { (make) in
            
            make.top.equalTo(dateView!.snp_bottom).offset(LDMargin_V*4)
            make.left.equalTo(LDMargin_V*4)
            make.right.equalTo(-LDMargin_V*4)
            make.height.equalTo(40)
            
        }
    }
}




